package init;


import java.sql.Connection;

import business.Utilerias;

//import oracle.jdbc.pool.OracleConnectionPoolDataSource;
//import oracle.jdbc.pool.OracleConnectionCacheImpl;
import oracle.jdbc.pool.OracleDataSource;

class Database {
	protected String SELECT = "SELECT";
	
	//static OracleConnectionPoolDataSource dsPool;
	//static OracleConnectionCacheImpl ds;
	private static OracleDataSource ds;
	
	private static String SERVER = "SERVER";
	private static String PORT = "PORT";
	private static String INSTANCE = "INSTANCE";
	
	private static Connection conn = null;
	private static String str_driver;
	private static String str_server;
	private static int int_port;
	private static String str_instanceDB;
	private static String str_username;
	private static String str_password;
	private static String str_urlFormat;
	private static String str_url;
	
	private static void createDataSource( ){
		try{
			/*dsPool = new OracleConnectionPoolDataSource();
			dsPool.setDriverType(Database.str_driver);
			dsPool.setURL(Database.str_url);
			dsPool.setUser(Database.str_username);
			dsPool.setPassword(Database.str_password);
			ds = new OracleConnectionCacheImpl(dsPool);
			ds.setMinLimit(3);
			ds.setMaxLimit(10);
			ds.setCacheScheme(OracleConnectionCacheImpl.FIXED_RETURN_NULL_SCHEME);*/
			ds = new OracleDataSource();
			ds.setDriverType(Database.str_driver);
			ds.setURL(Database.str_url);
			ds.setUser(Database.str_username);
			ds.setPassword(Database.str_password);
			ds.setPortNumber(Database.int_port);
			ds.setConnectionCachingEnabled(true);
    	}catch( Exception ex ){
    		ds = null;
    		throw new RuntimeException( ex.getMessage() );
    	}
	}
    
    protected static void instancia(String str_driver, String str_urlFormat, String str_server, int int_port, String str_instanceDB, String str_username, String str_password ) {
    	Database.str_driver = str_driver;
    	Database.str_urlFormat = str_urlFormat;
		
    	Database.str_server = str_server;
    	Database.int_port = int_port;
    	Database.str_instanceDB = str_instanceDB;
    	
    	Database.str_url = Database.replaceFormat(SERVER, Database.str_server);
    	Database.str_url = Database.replaceFormat(PORT, Database.int_port);
    	Database.str_url = Database.replaceFormat(INSTANCE, Database.str_instanceDB);
		
    	Database.str_username = str_username;
    	Database.str_password = str_password;
    	
    	createDataSource();
	}

	private static String replaceFormat( String str_search, int int_replace ){
    	return replaceFormat( str_search, Integer.toString( int_replace ) );
    } 
    
    private static String replaceFormat( String str_search, String str_replace ){
    	str_urlFormat = str_urlFormat.replaceAll(str_search, str_replace); 
    	return str_urlFormat; 
    }

    protected static Connection getConnection(){
    	Connection conn_retVal = null;
    	try{
    		if( conn == null){
    			if( ds == null ){
    				createDataSource();
    			}
    			conn = ds.getConnection();
    		}else{
    			if( conn.isClosed() ){
    				if( ds == null ){
    					createDataSource();
    				}
    				conn = ds.getConnection();
    	    	}
    		}
    		conn_retVal = conn;
    	}catch( Exception ex ){
    		System.out.println(Utilerias.sistemaLog("Error: "+ex.getMessage()));
    		throw new RuntimeException( ex.getMessage() );
    	}
    	return conn_retVal;
    }
    
    public static void closeConnection(){
    	try{
    		ds.close();
    	}catch(Exception ex){
    		
    	}
    }
}
